package com.example.fw;

import java.util.List;
import org.hibernate.Session;


public class HibernateHelper extends HelperBase{

	public HibernateHelper(ApplicationManager manager) {
		super(manager);
	}

	public String getLastGroupID(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		return session.createQuery("select max(id) from GroupObject").uniqueResult().toString();
	}
	
	public Groups getGroups() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		org.hibernate.Transaction tx = session.beginTransaction();
		Groups groups = null;
		try
		{
			groups = new Groups((List<GroupObject>) session.createQuery("from GroupObject").list());
		}
		finally
		{
			tx.commit();
		}
		return groups;
	}
	
	public List<ContactObject> getContacts() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		org.hibernate.Transaction tx = session.beginTransaction();
		List<ContactObject> contacts = null;
		try
		{
			contacts = (List<ContactObject>) session.createQuery("from ContactObject").list();
		}
		finally
		{
			tx.commit();
		}
		return contacts;
	}
	
	public void clearSession()
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.close();
	}
}
